package com.mr3chi.horoscopes;

import com.mr3chi.horoscopes.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class NavDrawerListAdapter extends ArrayAdapter<NavDrawerItem>{
	
	private LayoutInflater inflater;
    
    public NavDrawerListAdapter(Context context, int textViewResourceId, NavDrawerItem[] objects ) {
        super(context, textViewResourceId, objects);
        this.inflater = LayoutInflater.from(context);
    }
 
    @Override
    public long getItemId(int position) {
        return position;
    }

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = null ;
        NavDrawerItem menuItem = this.getItem(position);
        if ( menuItem.getType() == NavMenuItem.ITEM_TYPE ) {
            view = getItemView(convertView, parent, menuItem );
        }
        else {
            view = getSectionView(convertView, parent, menuItem);
        }
        return view;
	}
	
	public View getItemView( View convertView, ViewGroup parentView, NavDrawerItem navDrawerItem ) {
        NavMenuItem menuItem = (NavMenuItem) navDrawerItem;
        NavMenuItemHolder navMenuItemHolder = null;
        
        if (convertView == null) {
            convertView = inflater.inflate( R.layout.item_drawer_item, parentView, false);
            ImageView 	iconView 	= (ImageView) convertView.findViewById( R.id.nav_item_icon);
            TextView 	labelView 	= (TextView) convertView.findViewById( R.id.nav_item_title);
            
            navMenuItemHolder = new NavMenuItemHolder();
            navMenuItemHolder.iconView = iconView;
            navMenuItemHolder.labelView = labelView;

            convertView.setTag(navMenuItemHolder);
        }

        if (navMenuItemHolder == null) {
            navMenuItemHolder = (NavMenuItemHolder)convertView.getTag();
        }
                    
        navMenuItemHolder.labelView.setText(menuItem.getLabel());
        navMenuItemHolder.iconView.setImageResource(menuItem.getIcon());
        
        return convertView ;
    }

    public View getSectionView(View convertView, ViewGroup parentView, NavDrawerItem navDrawerItem) {
        NavMenuSection menuSection = (NavMenuSection) navDrawerItem ;
        NavMenuSectionHolder navMenuItemHolder = null;
        
        if (convertView == null) {
            convertView = inflater.inflate( R.layout.item_drawer_section, parentView, false);
            TextView labelView = (TextView) convertView.findViewById( R.id.navmenusection_label);

            navMenuItemHolder = new NavMenuSectionHolder();
            navMenuItemHolder.labelView = labelView;
            convertView.setTag(navMenuItemHolder);
        }

        if ( navMenuItemHolder == null ) {
            navMenuItemHolder = (NavMenuSectionHolder) convertView.getTag();
        }
                    
        navMenuItemHolder.labelView.setText(menuSection.getLabel());
        
        return convertView;
    }
    
    @Override
    public int getViewTypeCount() {
        return 2;
    }
    
    @Override
    public int getItemViewType(int position) {
        return this.getItem(position).getType();
    }
    
    @Override
    public boolean isEnabled(int position) {
        return getItem(position).isEnabled();
    }
    
    private static class NavMenuItemHolder {
    	private ImageView iconView;
        private TextView labelView;
    }
    
    private class NavMenuSectionHolder {
        private TextView labelView;
    }
}
